package com.jeecg.p3.commonluckymoney.service.impl;

import com.jeecg.p3.baseApi.service.BaseApiActTxtService;
import com.jeecg.p3.baseApi.util.WxActReplaceUtil;
import com.jeecg.p3.commonluckymoney.dao.WxActCommonluckymoneyAwardsDao;
import com.jeecg.p3.commonluckymoney.dao.WxActCommonluckymoneyDao;
import com.jeecg.p3.commonluckymoney.def.CommonluckymoneyProperties;
import com.jeecg.p3.commonluckymoney.entity.WxActCommonluckymoney;
import com.jeecg.p3.commonluckymoney.entity.WxActCommonluckymoneyAwards;
import com.jeecg.p3.commonluckymoney.service.WxActCommonluckymoneyService;
import org.jeecgframework.p3.core.utils.common.*;
import org.jeecgframework.p3.core.utils.persistence.OptimisticLockingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service("wxActCommonluckymoneyService")
public class WxActCommonluckymoneyServiceImpl implements WxActCommonluckymoneyService {
	@Resource
	private WxActCommonluckymoneyDao wxActCommonluckymoneyDao;
	@Resource
	private WxActCommonluckymoneyAwardsDao wxActCommonluckymoneyAwardsDao;
	@Autowired
	private BaseApiActTxtService baseApiActTxtService;

	@Override
	@Transactional(rollbackFor=Exception.class)
	public void doAdd(WxActCommonluckymoney wxActCommonluckymoney) {
		wxActCommonluckymoney.setProjectCode("commonluckymoney");
		WxActReplaceUtil.replace(wxActCommonluckymoney.getDetail());
		wxActCommonluckymoneyDao.insert(wxActCommonluckymoney);
		List<WxActCommonluckymoneyAwards> wxActCommonluckymoneyAwardsList = wxActCommonluckymoney.getCommonluckymoneyAwardsList();
		for (WxActCommonluckymoneyAwards wxActCommonluckymoneyAwards:wxActCommonluckymoneyAwardsList) {
			wxActCommonluckymoneyAwards.setActId(wxActCommonluckymoney.getId());
			wxActCommonluckymoneyAwards.setJwid(wxActCommonluckymoney.getJwid());
			wxActCommonluckymoneyAwards.setCreateTime(new Date());
			if(wxActCommonluckymoneyAwards.getRemainNum()==null){
				wxActCommonluckymoneyAwards.setRemainNum(0);
			}
			wxActCommonluckymoneyAwards.setRemainNum(wxActCommonluckymoneyAwards.getTotalNum());
		}
		for (WxActCommonluckymoneyAwards wxActCommonluckymoneyAwards : wxActCommonluckymoneyAwardsList) {
			wxActCommonluckymoneyAwardsDao.insert(wxActCommonluckymoneyAwards);
		}
		baseApiActTxtService.copyActText(CommonluckymoneyProperties.TXT_ACTID_KEY, wxActCommonluckymoney.getId());
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public String doEdit(WxActCommonluckymoney wxActCommonluckymoney) {
		String msg="";
		WxActReplaceUtil.replace(wxActCommonluckymoney.getDetail());
		wxActCommonluckymoneyDao.update(wxActCommonluckymoney);
		List<WxActCommonluckymoneyAwards> wxActCommonluckymoneyAwardsList = wxActCommonluckymoney.getCommonluckymoneyAwardsList();
		List<String> ids=new ArrayList<String>();
		if(wxActCommonluckymoneyAwardsList!=null){
			//先取出存在的Id
			for(WxActCommonluckymoneyAwards wxActCommonluckymoneyAwards:wxActCommonluckymoneyAwardsList){
				if(StringUtils.isNotEmpty(wxActCommonluckymoneyAwards.getId())){
					//更新语句
					WxActCommonluckymoneyAwards commonluckymoneyAwards = wxActCommonluckymoneyAwardsDao.get(wxActCommonluckymoneyAwards.getId());
					Integer num=0;
					if(!wxActCommonluckymoneyAwards.getTotalNum().equals(commonluckymoneyAwards.getTotalNum())){
						//修改后总数必须大于已中奖的数量
						Integer outNum = commonluckymoneyAwards.getTotalNum()-commonluckymoneyAwards.getRemainNum();
						Integer newTotalNum = wxActCommonluckymoneyAwards.getTotalNum();
						if(newTotalNum<outNum){
							msg = msg + commonluckymoneyAwards.getAwardsName()+"奖品数量不得小于已抽出数量 ："+outNum+";</br>";
						    return msg;
						}
						num=wxActCommonluckymoneyAwards.getTotalNum()-commonluckymoneyAwards.getTotalNum();
					}
					int row = wxActCommonluckymoneyAwardsDao.updateAwardsNum(wxActCommonluckymoneyAwards.getId(), num);
					if (row == 0) {
						throw new OptimisticLockingException("乐观锁异常");
					}

					wxActCommonluckymoneyAwardsDao.update(wxActCommonluckymoneyAwards);
					ids.add(wxActCommonluckymoneyAwards.getId());
				}
			}
			//List<WxActCommonluckymoneyAwards> deleteAwardsList=wxActCommonluckymoneyAwardsDao.queryByActIdAndIds(ids,wxActCommonluckymoney.getId());
			wxActCommonluckymoneyAwardsDao.bactchDeleteOldAwards(ids, wxActCommonluckymoney.getId());
			for(WxActCommonluckymoneyAwards wxActCommonluckymoneyAwards:wxActCommonluckymoneyAwardsList){
				if(StringUtils.isEmpty(wxActCommonluckymoneyAwards.getId())){
					wxActCommonluckymoneyAwards.setActId(wxActCommonluckymoney.getId());
					wxActCommonluckymoneyAwards.setJwid(wxActCommonluckymoney.getJwid());
					wxActCommonluckymoneyAwards.setCreateTime(new Date());
					if(wxActCommonluckymoneyAwards.getRemainNum()==null){
						wxActCommonluckymoneyAwards.setRemainNum(0);
					}
					wxActCommonluckymoneyAwards.setRemainNum(wxActCommonluckymoneyAwards.getTotalNum());
					wxActCommonluckymoneyAwardsDao.insert(wxActCommonluckymoneyAwards);
				}
			}
		}else{
			wxActCommonluckymoneyAwardsDao.bactchDeleteOldAwards(ids, wxActCommonluckymoney.getId());
		}
		return msg;
	}

	@Override
	@Transactional(rollbackFor=Exception.class)
	public void doDelete(String id) {
		wxActCommonluckymoneyDao.delete(id);
		baseApiActTxtService.batchDeleteByActCode(id);
	}

	@Override
	public WxActCommonluckymoney queryById(String id) {
		WxActCommonluckymoney wxActCommonluckymoney  = wxActCommonluckymoneyDao.get(id);
		return wxActCommonluckymoney;
	}

	@Override
	public PageList<WxActCommonluckymoney> queryPageList(
		PageQuery<WxActCommonluckymoney> pageQuery) {
		PageList<WxActCommonluckymoney> result = new PageList<WxActCommonluckymoney>();
		Integer itemCount = wxActCommonluckymoneyDao.count(pageQuery);
		PageQueryWrapper<WxActCommonluckymoney> wrapper = new PageQueryWrapper<>(pageQuery.getPageNo(), pageQuery.getPageSize(),itemCount, pageQuery.getQuery());
		List<WxActCommonluckymoney> list = wxActCommonluckymoneyDao.queryPageList(wrapper);
		Pagenation pagenation = new Pagenation(pageQuery.getPageNo(), itemCount, pageQuery.getPageSize());
		result.setPagenation(pagenation);
		result.setValues(list);
		return result;
	}

	@Override
	public void update(WxActCommonluckymoney wxActCommonluckymoney) {
		wxActCommonluckymoneyDao.update(wxActCommonluckymoney);
	}
}
